/**
 * @file
 * <pre> CPE 464 Winter 2012
 * -------------------
 *
 *  Header to "progrma header"
 *
 *  Last Modified: Sun May 13 04:26:35 AM PDT 2012</pre>
 *  @author Matthew Tondreau
 */

#ifndef _UTILITY_H_
#define _UTILITY_H_

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#pragma pack(1)

/** Message Format:
 * 0               1               2               3               4
 * | - - - | - - - | - - - | - - - | - - - | - - - | - - - | - - - |
 * |_______________________________________________________________|
 * |   Version #   |     Sender    |             type              |
 * |_______________|_______________|_______________________________|
 * |             flags             |             size              |
 * |_______________________________|_______________________________|
 * |                           reserved                            |
 * |_______________________________________________________________|
 * |                           Message                             |
 * ~                                                               ~
 * ~                                                               ~
 * |_______________________________________________________________|
 *
 * Version: The senders version of sec-chat.
 * Sender:  The senders 8-bit User ID.
 * Size:    Size of the Message field.
 * Type:    Type field for the message.
 *         _______________________________________________________________
 *        |  Type   | type code |                 definition              |
 *        |_________|___________|_________________________________________|
 *        | NOTICE  |   0x0000  | Notification from server such           |
 *        |         |           |      as shutting down or someone leaving|
 *        |         |           |      or entering the channel.           |
 *        | BMSG    |   0x0001  | Broadcast Message to everyone in the    |
 *        |         |           |      channel                            |
 *        | WHISPER |   0x0002  | Unicat message only to the user only    |
 *        |_________|___________|_________________________________________|
 *
 * Notice Message Format:
 *        _________________________________________________________________
 *       | Type of notice       | flag code |            Message           |
 *       |______________________|___________|______________________________|
 *       | Other Logout Notice  |  0x0000   | Username of logged out user  |
 *       | Other Login Notice   |  0x0001   | Username of logged in user   |
 *       | User Logout Notice   |  0x0002   | Resered                      |
 *       | User Login Notice    |  0x0003   | Reserved                     |
 *       | Refused Notice       |  0xFFFF   | Reason                       |
 *       | Idle Warning Notice  |  0xF000   | Time until user kicked       |
 *       |______________________|___________|______________________________|
 *
 * Broadcast Message Format:
 *       Message contains the message to be broadcasted.
 *       TODO: Consider compression mechanisms
 *
 * Whisper Message FOrmat:
 *       First byte in message contains User Id of who to whisper to
 *       remainder contains the message to whisper
 *
 */
#define VERSION   0x0000

#define NOTICE    0x0000
#define BMSG      0x0001
#define WHISPER   0x0002

#define N_OLOGOUT 0x0000
#define N_OLOGIN  0x0001
#define N_ULOGOUT 0x0002
#define N_ULOGIN  0x0003
#define N_REFUSED 0xFFFF
#define N_IDLE    0xF000


#define UNAME_SIZE 20
#define MAX_MESSAGE 256
#define MESSAGES_SIZE (MAX_MESSAGE + sizeof(Message))
#define MESSAGE_HEAD_SIZE (sizeof(Message) - MAX_MESSAGE)
typedef struct {
   uint8_t version;    /** Current version of sec-chat */
   uint8_t sender;      /** uid of the message */
   uint16_t type;       /** Type of message */
   uint16_t flags;      /** undefined */
   uint16_t size;
   uint16_t reserved[2];
   char     message[MAX_MESSAGE];
} Message;

int select_call(int32_t sk, int32_t seconds, int32_t usec);
int send_mesg( int32_t sk, uint16_t type, uint16_t flags, 
      uint16_t size, uint16_t uid, const char* buf);
int recv_mesg( int32_t sk, Message &m ); 

#endif
